home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 with MFC / Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso / CODE / Chap03 / MouseCap / MouseCap.cpp next >
Encoding:
C/C++ Source or Header  |  1996-04-05  |  2.6 KB  |  107 lines

  1. //***********************************************************************
  2. //
  3. //  MouseCap.cpp
  4. //
  5. //***********************************************************************
  6.  
  7. #include <afxwin.h>
  8. #include "MouseCap.h"
  9.  
  10. CMyApp myApp;
  11.  
  12. /////////////////////////////////////////////////////////////////////////
  13. // CMyApp member functions
  14.  
  15. BOOL CMyApp::InitInstance ()
  16. {
  17.     m_pMainWnd = new CMainWindow;
  18.     m_pMainWnd->ShowWindow (m_nCmdShow);
  19.     m_pMainWnd->UpdateWindow ();
  20.     return TRUE;
  21. }
  22.  
  23. /////////////////////////////////////////////////////////////////////////
  24. // CMainWindow message map and member functions
  25.  
  26. BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
  27.     ON_WM_LBUTTONDOWN ()
  28.     ON_WM_LBUTTONUP ()
  29.     ON_WM_MOUSEMOVE ()
  30.     ON_WM_NCLBUTTONDOWN ()
  31. END_MESSAGE_MAP ()
  32.  
  33. CMainWindow::CMainWindow ()
  34. {
  35.     m_bTracking = FALSE;
  36.     m_bCaptureEnabled = TRUE;
  37.  
  38.     CString strWndClass = AfxRegisterWndClass (
  39.         0,
  40.         myApp.LoadStandardCursor (IDC_CROSS),
  41.         (HBRUSH) (COLOR_WINDOW + 1),
  42.         myApp.LoadStandardIcon (IDI_APPLICATION)
  43.     );
  44.  
  45.     Create (strWndClass, "Mouse Capture Demo (Capture Enabled)");
  46. }
  47.  
  48. void CMainWindow::OnLButtonDown (UINT nFlags, CPoint point)
  49. {
  50.     m_ptFrom = point;
  51.     m_ptTo = point;
  52.     m_bTracking = TRUE;
  53.  
  54.     if (m_bCaptureEnabled)
  55.         SetCapture ();
  56. }
  57.  
  58. void CMainWindow::OnMouseMove (UINT nFlags, CPoint point)
  59. {
  60.     if (m_bTracking) {
  61.         CClientDC dc (this);
  62.         InvertLine (&dc, m_ptFrom, m_ptTo);
  63.         InvertLine (&dc, m_ptFrom, point);
  64.         m_ptTo = point;
  65.     }
  66. }
  67.  
  68. void CMainWindow::OnLButtonUp (UINT nFlags, CPoint point)
  69. {
  70.     if (m_bTracking) {
  71.         m_bTracking = FALSE;
  72.         if (GetCapture () == this)
  73.             ReleaseCapture ();
  74.  
  75.         CClientDC dc (this);
  76.         InvertLine (&dc, m_ptFrom, m_ptTo);
  77.  
  78.         CPen pen (PS_SOLID, 16, RGB (255, 0, 0));
  79.         dc.SelectObject (&pen);
  80.  
  81.         dc.MoveTo (m_ptFrom);
  82.         dc.LineTo (point);
  83.     }
  84. }
  85.  
  86. void CMainWindow::OnNcLButtonDown (UINT nHitTest, CPoint point)
  87. {
  88.     if (nHitTest == HTCAPTION) {
  89.         m_bCaptureEnabled = m_bCaptureEnabled ? FALSE : TRUE;
  90.  
  91.         SetWindowText (m_bCaptureEnabled ?
  92.             "Mouse Capture Demo (Capture Enabled)" :
  93.             "Mouse Capture Demo (Capture Disabled)");
  94.     }
  95.     CFrameWnd::OnNcLButtonDown (nHitTest, point);
  96. }
  97.  
  98. void CMainWindow::InvertLine (CDC* pDC, CPoint ptFrom, CPoint ptTo)
  99. {
  100.     int nOldMode = pDC->SetROP2 (R2_NOT);
  101.  
  102.     pDC->MoveTo (ptFrom);
  103.     pDC->LineTo (ptTo);
  104.  
  105.     pDC->SetROP2 (nOldMode);
  106. }
  107.